import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/events.dart';
import 'package:flame/text.dart' show TextStyle;
import 'package:flutter/animation.dart';
import 'package:flutter_flame/component/SpriteEntity.dart';
import 'package:flutter_flame/config/AssetsName.dart';

class MenuButton extends PositionComponent with TapCallbacks {
  MenuButton();

  final menuButtonDownLeft = _MenuButtonDownLeft();
  final menuButtonDownMiddle = _MenuButtonDownMiddle();
  final menuButtonDownRight = _MenuButtonDownRight();
  final text = _MenuText(Vector2(65, 20));
  final textClick = _MenuText(Vector2(66, 21));
  static const double _leftOffsetX = 1000.0;
  static const double _offsetY = 0.0;

  @override
  Future<void> onLoad() async {
    addAll([
      _MenuButtonLeft(),
      _MenuButtonMiddle(),
      _MenuButtonRight(),
    ]);
    position = Vector2(1350, 0);
    add(text);
    return super.onLoad();
  }

  show() {
    addAll([
      menuButtonDownLeft,
      menuButtonDownMiddle,
      menuButtonDownRight,
      textClick,
    ]);
  }

  hide() {
    removeAll([
      menuButtonDownLeft,
      menuButtonDownMiddle,
      menuButtonDownRight,
      textClick,
    ]);
  }

  void readyForFight() {
    add(
      MoveToEffect(
        Vector2(_leftOffsetX, _offsetY),
        EffectController(
          duration: 2,
          curve: Curves.easeInOut,
        ),
      ),
    );
  }
}

class _MenuText extends TextComponent {
  final TextStyle? style = TextStyle(
    color: Color(0xff5bc73b),
    fontSize: 25,
    fontWeight: FontWeight.bold,
  );
  final Vector2 setPosition;

  _MenuText(this.setPosition);

  @override
  Future<void> onLoad() async {
    text = "菜单";
    position = setPosition;
    textRenderer = TextPaint(
      style: style,
    );
  }
}

class _MenuButtonDownLeft extends SpriteEntity {
  _MenuButtonDownLeft() : super(AssetsName.buttonDownLeft);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    setSize(62);
    priority = menuPriority;
    position = Vector2(0, 0);
  }
}

class _MenuButtonDownMiddle extends SpriteEntity {
  _MenuButtonDownMiddle() : super(AssetsName.buttonDownMiddle);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    setSize(80);
    position = Vector2(60, 0);
  }
}

class _MenuButtonDownRight extends SpriteEntity {
  _MenuButtonDownRight() : super(AssetsName.buttonDownRight);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    setSize(60);
    position = Vector2(130, 0);
  }
}

class _MenuButtonLeft extends _MenuButtonModel {
  _MenuButtonLeft() : super(AssetsName.buttonLeft, 62, Vector2(0, 0));
}

class _MenuButtonMiddle extends _MenuButtonModel {
  _MenuButtonMiddle() : super(AssetsName.buttonMiddle, 80, Vector2(60, 0));
}

class _MenuButtonRight extends _MenuButtonModel {
  _MenuButtonRight() : super(AssetsName.buttonRight, 60, Vector2(130, 0));
}

class _MenuButtonModel extends SpriteEntity with TapCallbacks {
  final double w;
  final Vector2 pos;

  _MenuButtonModel(super.sprite, this.w, this.pos);

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    setSize(w);
    position = pos;
  }

  @override
  void onTapDown(TapDownEvent event) {
    (parent as MenuButton).show();
    super.onTapDown(event);
  }

  @override
  void onTapUp(TapUpEvent event) {
    (parent as MenuButton).hide();
    super.onTapUp(event);
  }
}
